﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;
using VocabManager.ViewModel;
using VocabManager.Voice;

namespace VocabManager.View
{
    public partial class ItemViewModeEdit : UserControlBase
    {
        ApplicationBarIconButton _cancel;
        ApplicationBarIconButton _save;
        ItemViewModel _itemViewModel;
        public ItemViewModeEdit()
        {
            InitializeComponent();
            _cancel = new ApplicationBarIconButton(new Uri(IconUrls.Cancel, UriKind.Relative));
            _cancel.Click += new EventHandler(_cancel_Click);
            _cancel.Text = "cancel";


            _save = new ApplicationBarIconButton(new Uri(IconUrls.Save, UriKind.Relative));
            _save.Text = "save";
            _save.Click += new EventHandler(_save_Click);

        }

        void _save_Click(object sender, EventArgs e)
        {
            FinishStartedRecording();

            _itemViewModel.SaveCommand.Execute(null);
        }

        void _cancel_Click(object sender, EventArgs e)
        {
            FinishStartedRecording();
            _itemViewModel.CancelEditCommand.Execute(null);
        }

        protected override void OnLoad()
        {
            base.OnLoad();
            _itemViewModel = this.DataContext as ItemViewModel;
            StoreAppBarButtons();
            ContainerPage.ApplicationBar.Buttons.Clear();

       

            ContainerPage.ApplicationBar.Buttons.Add(_save);
            ContainerPage.ApplicationBar.Buttons.Add(_cancel);

        

        }
        protected override void OnUnload()
        {
            base.OnUnload();
            FinishStartedRecording();
        }

        private void FinishStartedRecording()
        {
            if (_soundManager != null && _soundManager.IsRecording)
            {
                StopRecording();

            }
        }


        SoundManager _soundManager;
        SoundManager SoundManager
        {
            get
            {
                if (_soundManager == null)
                {

                    if (!string.IsNullOrEmpty(_itemViewModel.BackSound))
                        _soundManager = new SoundManager(_itemViewModel.BackSound);
                    else
                        _soundManager = new SoundManager();
                }
                return _soundManager;
 
            }
        }
        private void voiceBtn_Click(object sender, RoutedEventArgs e)
        {


            if (!SoundManager.IsRecording)
            {
                StartRecording();
             

            }
            else
            {
                StopRecording();
            }
        }

        private void StopRecording()
        {
            SoundManager.RequestStopRecording();
            voiceBtn.Content = "Start Recording";
            voiceBtn.Foreground = new SolidColorBrush(Colors.White);

            _itemViewModel.BackSound = _soundManager.FileName;
        }

        private void StartRecording()
        {
            SoundManager.StartRecording();
            voiceBtn.Content = "Stop Recording";
            voiceBtn.Foreground = new SolidColorBrush(Colors.Red);
        }



    }
}
